<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\GoodsAttribute;
use App\Models\GoodsType;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests\Admin\goodsAttribute\StoreRequest;
use App\Http\Requests\Admin\goodsAttribute\UpdateRequest;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;

class GoodsAttributeController extends Controller
{
    public function goods_attribute_gate()
    {
        if (!Gate::allows('goods','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->goods_attribute_gate();

        //dd($goodsType['type_name']);
        $goodsType = GoodsType::get()->toArray();
        //dd($goodsType);
        $type_name = $request->get('type_name');
        //dd($request->all());

        $goodsAttribute = GoodsAttribute::orderBy('sort_order','desc')->paginate(10);
        //dd($goodsAttribute);
        //$goodsAttribute = GoodsAttribute::paginate(10);
        return view('admin.goods_attribute.index',compact('goodsAttribute','type_name','goodsType'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(GoodsAttribute $goodsAttribute)
    {
        $this->goods_attribute_gate();

        return view('admin.goods_attribute.create',compact('goodsAttribute'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRequest $request , GoodsAttribute $goodsAttribute)
    {
        $this->goods_attribute_gate();

        $param = $request->all();
        //dd($param);
        $goodsAttribute->create($param);
        return Redirect('admin/goods_attribute');
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {

        $this->goods_attribute_gate();

        $goodsAttribute = GoodsAttribute::find($id);
        return view('admin.goods_attribute.edit',compact('goodsAttribute'));
    }

    public function update(UpdateRequest $request, $id)
    {
        $this->goods_attribute_gate();

        $goodsAttribute = GoodsAttribute::find($id);        

        $goodsAttribute->update($request->all());

        return Redirect('admin/goods_attribute');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->goods_attribute_gate();

        $return = ['status'=>0, 'message'=>'', 'data'=>''];

        $goodsAttribute = GoodsAttribute::find($id);

        $goodsAttribute->delete();

        return $return;
    }

    /**
     * 排序
     * @route admin.goods_attribute.sort
     * @return Response
     */
    public function sort(Request $request)
    {
        $this->goods_attribute_gate();

        //dd($request->all());
        $goodsAttribute = $request->get('sort_order');
        //dd($goodsAttribute);

        if (is_array($goodsAttribute)&&count($goodsAttribute)>0)
        {
            foreach($goodsAttribute as $k=>$v)
            {
                //dd($v);
                $sort_order = GoodsAttribute::where('sort_order',$v)->get();
                //$sort_order=GoodsAttribute::find('sort_order');
                //dd($sort_order);
                GoodsAttribute::find($k)->update(['sort_order' => $v]);


            }
        }

        return redirect('admin/goods_attribute');

    }
}
